package com.jn.gsondemo.adapter;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.gsondemo.R;
import com.jn.gsondemo.entity.HotSearchItem;

import java.util.List;

public class HotSearchAdapter extends RecyclerView.Adapter<HotSearchAdapter.HotSearchViewHolder> {
    private List<HotSearchItem> hotSearchItems;

    public HotSearchAdapter(List<HotSearchItem> hotSearchItems) {
        this.hotSearchItems = hotSearchItems;
    }

    @NonNull
    @Override
    public HotSearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hot_search, parent, false);
        return new HotSearchViewHolder(view);
    }



    @Override
    public void onBindViewHolder(@NonNull HotSearchViewHolder holder, int position) {
        HotSearchItem item = hotSearchItems.get(position);
        holder.titleTextView.setText(item.getTitle());
        holder.hotValueTextView.setText(item.getHotValue());
        holder.itemView.setOnClickListener(v -> {
            // 处理点击事件，打开链接
            Uri uri = Uri.parse(item.getLink());
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            holder.itemView.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return hotSearchItems.size();
    }

    static class HotSearchViewHolder extends RecyclerView.ViewHolder {
        TextView titleTextView;
        TextView hotValueTextView;

        HotSearchViewHolder(View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.title_text_view);
            hotValueTextView = itemView.findViewById(R.id.hot_value_text_view);
        }
    }
}